﻿using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.Text;
using System.Threading.Tasks;
using Samples.DiscoveryServices.ServiceContracts;

namespace Samples.DisconveryClient.ConsoleHost
{
    using System;

    class Program
    {
        static void Main(string[] args)
        {
            ChannelFactory<ISimplestFeaturesServiceContract> channelFactory =
                new ConfigurationChannelFactory<ISimplestFeaturesServiceContract>("DynamicService", LoadHostConfiguration(), null);

            ISimplestFeaturesServiceContract service = channelFactory.CreateChannel();

            Console.WriteLine(service.SayHello("Wang Yucai"));

            Console.ReadLine();
        }

        /// <summary>
        ///     从Host.config文件中加载服务配置。
        /// </summary>
        /// <returns>
        ///     <see cref="Configuration"/>对象实例。
        /// </returns>
        private static Configuration LoadHostConfiguration()
        {
            ExeConfigurationFileMap hostConfigurationFileMap = new ExeConfigurationFileMap()
            {
                ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"Host.config")
            };
            return ConfigurationManager.OpenMappedExeConfiguration(hostConfigurationFileMap, ConfigurationUserLevel.None);
        }
    }
}
